package com.gitee.qdbp.base.servlet;

import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.gitee.qdbp.general.common.biz.files.service.LocalFileService;

public class InitServlet extends HttpServlet {

    /** 版本序列号 **/
    private static final long serialVersionUID = -8452019304732100649L;

    @Override
    public void init() throws ServletException {

        ServletContext context = getServletConfig().getServletContext();

        String webRootPath = context.getRealPath("/");
        // String contextPath = context.getContextPath();

        // 为LocalFileService设置WebRootPath
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        try {
            Map<String, LocalFileService> services = ctx.getBeansOfType(LocalFileService.class);
            if (services != null && !services.isEmpty()) {
                for (LocalFileService fs : services.values()) {
                    fs.setWebRootPath(webRootPath);
                }
            }
        } catch (NoSuchBeanDefinitionException ignore) {
        }
    }

}
